import { stringify } from 'qs';
import request from '../utils/request';
import { ipConfig } from './config';

// http://mesdev.chalco-steering.com:1708/appgateway/api-ms/pub/todo/search?appId=APP_MES&status=2&page=9&rows=10&type=1

// export const serverName = '/api-pea-device-170236';
export const serverName = ipConfig.baseServerName();

export const imageUrl = `${serverName}/logistics/fileController/download/`; // 图片路径

const techPerformanceUrl = `${serverName}/performanceItem/findByOjb`; // 技术性能
const getManualUrl = `${serverName}/tdDoc/findByObjType`; // 技术手册
const tdLubricationUrl = `${serverName}/tdLubrication/findByObjType`; // 润滑标准
const getCorollaryEquipmentUrl = `${serverName}/device/equipment/findByMainequipId`; // 配套设备
const getChangeRecordUrl = `${serverName}/alteration/findByEquipmentId`; // 变更记录
const getChangeHistoryUrl = `${serverName}/device/accyreplacement/findByEquipmentId`; // 更换历史

export const getTechPerformance = async (params) => {
  return request(`${ipConfig.url(techPerformanceUrl)}?${stringify(params)}`, {
    method: 'GET',
  }).then(response => response.data);
};

export const getManual = async (params) => {
  return request(`${ipConfig.url(getManualUrl)}?${stringify(params)}`, {
    method: 'GET',
  }).then(mapGetManual);
};

export const tdLubrication = async (params) => {
  return request(`${ipConfig.url(tdLubricationUrl)}?${stringify(params)}`, {
    method: 'GET',
  }).then(response => response.data);
};

export const getCorollaryEquipment = async (params) => {
  return request(`${ipConfig.url(getCorollaryEquipmentUrl)}?${stringify(params)}`, {
    method: 'GET',
  }).then(response => response.data);
};

export const getChangeRecord = async (params) => {
  return request(`${ipConfig.url(getChangeRecordUrl)}?${stringify(params)}`, {
    method: 'GET',
  }).then(response => response.data);
};

export const getChangeHistory = async (params) => {
  return request(`${ipConfig.url(getChangeHistoryUrl)}?${stringify(params)}`, {
    method: 'GET',
  }).then(response => response.data);
};

// export const addEntry = async params => {
//   return request(`${ipConfig.url(addEntryUrl)}`, {
//     method: 'POST',
//     body: params,
//   }).then(response => response.data);
// };

// const mapCheckExist = response => {
//   return {
//     data: response.data,
//   };
// };

const mapGetManual = response => {
  let data;
  if (response.data) {
    data = {
      maintenanceManual: response.data.maintenanceManual && `${ipConfig.url(imageUrl)}${response.data.maintenanceManual}?token=${global.__TOKEN_ACCESS__}`,
      operationManual: response.data.operationManual && `${ipConfig.url(imageUrl)}${response.data.operationManual}?token=${global.__TOKEN_ACCESS__}`,
    };
  }
  // `http://gateway.pea.cs2025.cn/gateway/api-pea-device/logistics/fileController/download/2c94b38a673f928801673fe2b5250011?token=${global.__TOKEN_ACCESS__}`

  return data;
};
